package cn.dy.controller;

import cn.dy.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;

import java.util.Date;

@Controller
@RequestMapping("/anno")
@SessionAttributes(value = {"msg"})//把msg=北京 存入session中
public class AnnoController {

    @RequestMapping("testRequestParam")
    public String testRequestParam(@RequestParam(value = "users",required = false) String username){
        System.out.println("执行了testRequestParam...");
        System.out.println(username);
        return "success";
    }

    @RequestMapping("testRequestBody")//键值对
    public String testRequestBody(@RequestBody String body){
        System.out.println("执行了testRequestParam");
        System.out.println(body);
        return "success";
    }

    @RequestMapping("testPathValb/{aa}")  //获取路径后边的   两个aa必须对应  String后无所谓
    public String testPathValb(@PathVariable (name = "aa") String aa){
        System.out.println("执行了testRequestParam");
        System.out.println(aa);
        return "success";
    }

    @RequestMapping("testRequestHeader")//获取头文件
    public String testRequestHeader(@RequestHeader(value = "Accept") String header){
        System.out.println("testRequestHeader");
        System.out.println(header);
        return "success";
    }

    @RequestMapping("testCookieValue")
    public String testCookieValue(@CookieValue (value ="JSESSIONID") String cookie){
        System.out.println("testCookieValue");
        System.out.println(cookie);
        return "success";
    }

//    @RequestMapping("testModelAttribute")
//    public String testModelAttribute(User user){
//        System.out.println("执行testModelAttribute");
//        System.out.println(user);
//        return "success";
//    }

//    @ModelAttribute
//    public User showUser(String uname){
//        User user=new User();
//        user.setUname(uname);
//        user.setAge(20);
//        user.setDate(new Date());
//        System.out.println("showUser执行了");
//        return user;
//    }

    @RequestMapping("testSessionAttributes")
    public String testSessionAttributes(Model model){
        System.out.println("执行了testSessionAttributes");
        model.addAttribute("msg","北京" );
        return "success";
    }

    @RequestMapping("getSessionAttribute")//获取session数据
    public String getSessionAttribute(ModelMap modelMap){
        String msg =(String) modelMap.get("msg");
        System.out.println(msg);
        return "success";

    }

    //清楚session数据
    @RequestMapping("delSessionAttribute")
    public String delSessionAttribute(SessionStatus sessionStatus){
        sessionStatus.setComplete();//删除session数据
        return "success";

    }



}
